import api from '@api'
const state = {
  list: {
    data: [],
    pageNum: 0,
    page: 1
  },
  detail: {
    id: '',
    shop: null,
    data: null,
  },
  rate: {
    id: '',
    shop: null,
    data: []
  }
}

const getters = {
  ['getList'] (state) {
    return state.list
  },
  ['getDetail'] (state) {
    return state.detail
  },
  ['getRate'] (state) {
    return state.rate
  }
}

const actions = {
  async ['getList'] ({commit}, page) {
    let config= {
      PageSize: 5,
      CurrentPage: page
    };
    let {data: {orderList, pageNum, StatusCode}} = await api.get('api/v2/Order/orderList.ashx', config);
    if(StatusCode == '200') {
      commit('setList', {data:orderList, pageNum: parseInt(pageNum), page})
    }
  },
  async ['getDetail'] ({commit, state}, orderId) {
    if(state.detail.id == orderId) {
      return
    }
    let {data} = await api.get('api/v2/Order/userOrderDetail.ashx', {orderSn: orderId})
    if(data.StatusCode == '200') {
      commit('setDetail', {
        id: orderId,
        shop: data.storeInfo[0],
        data: data
      })
    }
  },
  async ['getRate'] ({commit, state}, orderId) {
    if(state.rate.id == orderId) {
      return
    }
    let {data: {OrderGoods, storeInfo, StatusCode}} = await api.get('api/v2/Evaluate/evalOrderInfo.ashx', {orderSn: orderId});
    if(StatusCode == '200') {
      commit('setRate', {
        id: orderId,
        shop: storeInfo[0],
        data: OrderGoods
      })
    }
    return 'ok';
  }
}

const mutations = {
  ['setList'] (state, {data, pageNum, page}) {
    state.list.data = page==1? data : state.list.data.concat(data);
    state.list.pageNum = pageNum;
    state.list.page = page;
  },
  ['clearList'] (state) {
    state.list= {
      data: [],
      pageNum: 0,
      page: 1
    }
  },
  ['setDetail'] (state, {id, shop, data}) {
    state.detail.id = id;
    state.detail.shop = shop;
    state.detail.data = data;
  },
  ['setRate'] (state, {id, shop, data}) {
    state.rate.id = id;
    state.rate.shop = shop;
    state.rate.data = data;
  }
}

export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations
}